package cc.avatar.model.inventory;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import cc.avatar.model.Location;
import cc.avatar.model.Product;

public class Inventory {
    private Product product;
    
    private int totalQuantity;
    
    private int totalQuantityForSale;
    
    private Map<Location, Integer> quantities = new LinkedHashMap<Location, Integer>();

    private Map<Location, Integer> quantitiesForSale = new LinkedHashMap<Location, Integer>();
    
	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}
	
	public int getTotalQuantity() {
		return totalQuantity;
	}

	public void setTotalQuantity(int totalQuantity) {
		this.totalQuantity = totalQuantity;
	}

	public int getTotalQuantityForSale() {
		return totalQuantityForSale;
	}

	public void setTotalQuantityForSale(int totalQuantityForSale) {
		this.totalQuantityForSale = totalQuantityForSale;
	}

	public Map<Location, Integer> getQuantities() {
		return quantities;
	}

	public void setQuantities(Map<Location, Integer> quantities) {
		this.quantities = quantities;
	}

	public Map<Location, Integer> getQuantitiesForSale() {
		return quantitiesForSale;
	}

	public void setQuantitiesForSale(Map<Location, Integer> quantitiesForSale) {
		this.quantitiesForSale = quantitiesForSale;
	}

	public void sum(List<InventoryChange> records){
		this.totalQuantity = 0;
		this.totalQuantityForSale = 0;
		
		this.quantities.clear();
		this.quantitiesForSale.clear();
		
		for (InventoryChange log: records){
			Location location = log.getLocation();
			Integer amount = this.quantities.get(location); 
			Integer amountAvailable = this.quantitiesForSale.get(location); 
			if (amount == null){
				amount = 0;
			}
			if (amountAvailable == null){
				amountAvailable = 0;
			}
			amount += log.getQuantity();
			
			this.totalQuantity += log.getQuantity();
			if (log.isAvailable()){
				this.totalQuantityForSale += log.getQuantity();
				amountAvailable += log.getQuantity();
			}
			
			this.quantities.put(location, amount);
			this.quantitiesForSale.put(location, amountAvailable);
		
		}
		if (this.totalQuantityForSale < 0)
			this.totalQuantityForSale = 0;
	}
	
	public List<LocatedInventory> getLocatedInventories(){
		List<LocatedInventory> results = new ArrayList<LocatedInventory>();
		for (Location loc : quantities.keySet()){
			int amount = quantities.get(loc);
			int amountSale = quantitiesForSale.get(loc);
			if (amount > 0 || amountSale > 0)
			    results.add(new LocatedInventory(loc, amount, amountSale));
		}
		return results;
	}
	
	public StockInLocation getStockInLocation(Location loc){ 
		Integer amount = findValueByLocId(loc.getId(), false);
		Integer amountSale = findValueByLocId(loc.getId(), true);
		if (amount != null || amountSale != null)
		    return new StockInLocation(this.product, amountSale, amount);
		else
			return null;	
	}
	
	private Integer findValueByLocId(Long id, boolean forSale){
		Map<Location, Integer> m = forSale?this.quantitiesForSale:this.quantities;
		for (Location loc : m.keySet()){
			if (loc.getId().equals(id)) return 
			  m.get(loc);
		}
		return null;
	}
}
